package com.gdsdxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gdsdxy.dto.LoginFormDTO;
import com.gdsdxy.dto.PwdFormDTO;
import com.gdsdxy.dto.ResultVo;
import com.gdsdxy.dto.UserDTO;
import com.gdsdxy.entity.User;
import com.gdsdxy.service.IUserService;
import com.gdsdxy.utils.UserHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import static com.gdsdxy.utils.UploadUtils.saveFile;

@RestController
@CrossOrigin
@RequestMapping("/users")
@Api(tags = "用户登录接口")
public class UserController {
    @Resource
    private IUserService userService;

    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public ResultVo register(@RequestBody User user) {
        return userService.register(user);
    }

    @GetMapping("/code")
    @ApiOperation(value = "登录：发送验证码")
    public ResultVo sendCode(@RequestParam("phone") String phone) {
        return userService.sendCode(phone);
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "可以账号与密码登录，也可以手机号与验证码登录，优先账号密码登录")
    public ResultVo login(@RequestBody LoginFormDTO loginFormDTO) {
        return userService.login(loginFormDTO);
    }

    @GetMapping("/me")
    @ApiOperation(value = "获取登录用户信息")
    public ResultVo me() {
        UserDTO user = UserHolder.getUser();
        return ResultVo.ok(user);
    }

    @GetMapping("/isPassword")
    @ApiOperation(value = "查询用户是否设置密码")
    public ResultVo isPassword() {
        return userService.isPassword();
    }

    @PutMapping("/setPassword")
    @ApiOperation(value = "设置/修改密码", notes = "当新用户未设置密码，则新密码(newPassword)作为密码；当用户已经设置密码，则需要旧密码(oldPassword)和新密码(newPassword)才能修改密码")
    public ResultVo setPassword(@RequestBody PwdFormDTO pwdFormDTO) {
        return userService.setPassword(pwdFormDTO);
    }

    @PostMapping("/uploadIcon")
    @ApiOperation(value = "上传用户头像", notes = "上传成功，返回头像存放路径")
    public ResultVo uploadIcon(@RequestPart("file") MultipartFile file) {
        // 保存文件
        return saveFile(file, "/user/icon");
    }

    @PutMapping("/updateIcon")
    @ApiOperation(value = "修改用户头像")
    public ResultVo updateIcon(@RequestBody UserDTO user) {
        return userService.updateIcon(user.getIcon());
    }

    @PutMapping("/updateNickName")
    @ApiOperation(value = "修改用户昵称")
    public ResultVo updateNickName(@RequestBody UserDTO user) {
        return userService.updateNickName(user.getNickname());
    }

    @GetMapping("/isNotUpdateAccount")
    @ApiOperation(value = "查看用户是否未修改过账号")
    public ResultVo isNotUpdateAccount() {
        return userService.isNotUpdateAccount();
    }

    @PutMapping("/updateAccount")
    @ApiOperation(value = "修改用户账号", notes = "仅未修改过账号的用户可修改一次账号")
    public ResultVo updateAccount(@RequestBody UserDTO user) {
        return userService.updateAccount(user.getAccount());
    }

    @DeleteMapping("/quit")
    @ApiOperation(value = "用户登出")
    public ResultVo logOut(@RequestHeader("authorization") String token) {
        return userService.logOut(token);
    }

    @GetMapping("/codePassword")
    @ApiOperation(value = "忘记密码：发送验证码")
    public ResultVo codePassword(@RequestParam("phone") String phone) {
        return userService.codePassword(phone);
    }

    @GetMapping("/check/codePassword")
    @ApiOperation(value = "忘记密码：验证验证码")
    public ResultVo checkCodePassword(@RequestParam("phone") String phone, @RequestParam("code") String code) {
        return userService.checkCodePassword(phone, code);
    }

    @PutMapping("/updatePassword")
    @ApiOperation(value = "忘记密码：修改密码", notes = "需要手机号(phone)与新密码(newPassword)")
    public ResultVo updatePassword(@RequestBody PwdFormDTO pwdFormDTO) {
        return userService.updatePassword(pwdFormDTO);
    }
}
